package cn.com.xinjinfu.pdoulcProduct.manager;

import java.io.Serializable;
import java.text.NumberFormat;
import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.com.xinjinfu.pdoulcProduct.R;
import cn.com.xinjinfu.pdoulcProduct.activity.recharge.ForgetPayPwdActivity;
import cn.com.xinjinfu.pdoulcProduct.activity.recharge.TradePWD;
import cn.com.xinjinfu.pdoulcProduct.entity.BanksCardBean;
import cn.com.xinjinfu.pdoulcProduct.view.CustomProgressDialog;
import cn.com.xinjinfu.pdoulcProduct.view.PwdEditText;

public class UIManager {


    /**
     * 网络访问时候的提示对话框
     *
     * @param context
     * @return
     */
    public static Dialog getLoadingDialog(Context context) {

//        return getLoadingDialog(context, R.string.dialog_loading);
        return getLoadingDialog1(context,"拼命加载中...");
    }

    public static Dialog getLoadingDialog(Context context, Object loadingTextRes) {
        final Dialog dialog = new Dialog(context, R.style.netLoadingDialog);
        dialog.setCancelable(false);
        dialog.setContentView(R.layout.custom_progress_dialog);
        Window window = dialog.getWindow();
        window.setGravity(Gravity.CENTER); // 此处可以设置dialog显示的位置
        dialog.setCanceledOnTouchOutside(true);
        return dialog;
    }


    public static Dialog getLoadingDialog1(Context context, String loadingTextRes) {
        CustomProgressDialog dialog =new CustomProgressDialog(context,loadingTextRes,R.anim.frameloading);
        dialog.setCancelable(false);
        Window window = dialog.getWindow();
        window.setGravity(Gravity.CENTER); // 此处可以设置dialog显示的位置
        dialog.setCanceledOnTouchOutside(false);
        return dialog;
    }

    /**
     * 通用的dialog提示框 （这个定义好了两个button的文字）
     *
     * @param context
     * @param contentRes
     * @param l
     * @return
     */

    public static Dialog getCommWarnDialog(Context context, int layout,
                                           int contentRes, final OnClickListener l) {
        return getCommWarnDialog(context, layout, contentRes, "确定", "取消", l);
    }

    /**
     * 通用的dialog提示框
     *
     * @param context
     * @param contentRes 提示正文内容
     * @param l          点击右边bt后的动作监听
     * @return
     */
    public static Dialog getCommWarnDialog(Context context, int layout,
                                           int contentRes, String x, String y, final OnClickListener l) {
        final Dialog dialog = new Dialog(context, R.style.dialog_float_base);
        View view = View.inflate(context, layout, null);
        TextView content = (TextView) view.findViewById(R.id.warn_content_tv);
        TextView sure = (TextView) view.findViewById(R.id.warn_sure_bt);
        TextView cancle = (TextView) view.findViewById(R.id.warn_cancle_bt);
        content.setText(contentRes);
        sure.setText(x);
        cancle.setText(y);
        dialog.setContentView(view);
        // 触摸对话框以外 dismiss对话框
        dialog.setCanceledOnTouchOutside(true);
        view.findViewById(R.id.warn_sure_bt).setOnClickListener(
                new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        l.onClick(v);
                    }
                });
        view.findViewById(R.id.warn_cancle_bt).setOnClickListener(
                new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                        l.onClick(v);
                    }
                });

        Window window = dialog.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = getScreenWidth(context) - dpToPx(context, 50);
        window.setGravity(Gravity.CENTER_VERTICAL); // 此处可以设置dialog显示的位置

        return dialog;
    }

    /**
     * 关闭dialog
     *
     * @param loadDialog
     */
    public static void toggleDialog(Dialog loadDialog) {
        if (loadDialog != null && loadDialog.isShowing()) {
            loadDialog.dismiss();
        }
    }

    /**
     * 选择好银行后的提示框 dialog
     */
    public static void showBankInfo(Context context, BanksCardBean.SupportBanks supportBanks, final OnClickListener l) {

        final Dialog dialog = new Dialog(context, R.style.dialog_tran);
        dialog.setContentView(R.layout.dialog_bank_shape);
        TextView tv_bankcart_info = (TextView) dialog.findViewById(R.id.tv_bankcart_info);
        String mBank = supportBanks.getBank_name();
        String f1 = NumberFormat.getInstance().format(supportBanks.getSingle_amt() / 10000);
        String f2 = NumberFormat.getInstance().format(supportBanks.getDay_amt() / 10000);
        String f3 = NumberFormat.getInstance().format(supportBanks.getMonth_amt() / 10000);
        SpannableString tSS = new SpannableString(mBank + "卡充值限额为单笔" + f1 + "万元，单日累计" + f2 + "万元，单月累计" + f3 + "万元，请您确认。");
        tSS.setSpan(new ForegroundColorSpan(0xFFF77F96), String.valueOf(mBank).length() + 8, String.valueOf(mBank).length() + 10
                + String.valueOf(f1).length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        tSS.setSpan(new ForegroundColorSpan(0xFFF77F96), String.valueOf(mBank).length() +
                        15 + String.valueOf(f1).length(),
                String.valueOf(mBank).length() + 17 + String.valueOf(f1).length()
                        + String.valueOf(f2).length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        tSS.setSpan(new ForegroundColorSpan(0xFFF77F96), String.valueOf(mBank).length() + 22 +
                        String.valueOf(f1).length() + String.valueOf(f2).length(),
                String.valueOf(mBank).length() + 24 + String.valueOf(f1).length() +
                        String.valueOf(f2).length() + String.valueOf(f3).length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv_bankcart_info.setText(tSS);
        dialog.findViewById(R.id.bt_queding).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (l!=null){
                    l.onClick(v);
                }
                dialog.dismiss();
            }
        });
        dialog.findViewById(R.id.img_search).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        dialog.show();
        dialog.setCanceledOnTouchOutside(false);
    }

    /**
     * 赎回dilog
     * @param context
     * @param f1
     * @param f2
     * @param f3
     * @param l1
     * @param l2
     */
    public static void showshuhuiInfo(Context context, String f1,String f2,String f3, final OnClickListener l1,final OnClickListener l2) {

        final Dialog dialog = new Dialog(context, R.style.dialog_tran);
        dialog.setContentView(R.layout.dialog_shuhui);
        TextView tv_bankcart_info = (TextView) dialog.findViewById(R.id.tv_bankcart_info);
        SpannableString tSS = new SpannableString("成功赎回" + f1 + "元至您的P兜账户，其中：收益" + f2 + "元；在投本金" + f3 + "元");
        tSS.setSpan(new ForegroundColorSpan(0xFFF77F96),4, 4 + String.valueOf(f1).length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        tSS.setSpan(new ForegroundColorSpan(0xFFF77F96),18 + String.valueOf(f1).length(),
                18+ String.valueOf(f1).length()+ String.valueOf(f2).length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        tSS.setSpan(new ForegroundColorSpan(0xFFF77F96), 24 + String.valueOf(f1).length() + String.valueOf(f2).length(),
                24 + String.valueOf(f1).length() + String.valueOf(f2).length() + String.valueOf(f3).length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv_bankcart_info.setText(tSS);
        dialog.findViewById(R.id.bt_tixian).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (l1!=null){
                    l1.onClick(v);
                }
                dialog.dismiss();
            }
        });
        dialog.findViewById(R.id.bt_chakan).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (l2!=null){
                    l2.onClick(v);
                }
                dialog.dismiss();
            }
        });
        dialog.show();
        dialog.setCanceledOnTouchOutside(false);
    }

    /***
     *   提现
     * @param context
     * @param f1
     * @param l
     */

    public static void showTrandDilog(Context context, String f1, final OnClickListener l) {
        final Dialog dialog = new Dialog(context, R.style.dialog_tran);
        dialog.setContentView(R.layout.dialog_bank_shape);
        TextView tv_bankcart_info = (TextView) dialog.findViewById(R.id.tv_bankcart_info);
        SpannableString tSS = new SpannableString("确认提现" + f1 + "元？");
        tSS.setSpan(new ForegroundColorSpan(0xFFF77F96), 4, String.valueOf(f1).length() + 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv_bankcart_info.setText(tSS);
        dialog.findViewById(R.id.bt_queding).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (l!=null){
                    l.onClick(v);
                }
                dialog.dismiss();
            }
        });
        dialog.findViewById(R.id.img_search).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        dialog.show();
        dialog.setCanceledOnTouchOutside(false);
    }



    /***
     *   服务器维护中
     */

    public static void showWeiHuDilog(Context mContext) {

    }


    /***
     *   通用单按钮（带取消键）单色字体
     *
     *   图像大小    109*115
     * @param context
     */

    public static void showSimpleDilog(Context context, String info,final OnClickListener l) {
        final Dialog dialog = new Dialog(context, R.style.dialog_tran);
        dialog.setContentView(R.layout.dialog_simple);
        TextView tv_info = (TextView) dialog.findViewById(R.id.tv_bankcart_info);
        tv_info.setText(info);
        ImageView imageView = (ImageView) dialog.findViewById(R.id.imageView);
//        imageView.setImageResource(R.drawable.img_doucry);
        dialog.findViewById(R.id.bt_queding).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (l != null) {
                    l.onClick(v);
                }
                dialog.dismiss();
            }
        });
        dialog.findViewById(R.id.img_search).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.show();
        dialog.setCanceledOnTouchOutside(false);
    }

    /**
     * 成功或失败的地龙
     *
     * @param context
     * @param info
     * @param code
     * @param l
     */
    public static void showSuccessFailDilog(final Activity context, String info, final int code, final OnClickListener l) {
        final Dialog dialog = new Dialog(context, R.style.dialog_tran);
        dialog.setContentView(R.layout.dialog_result_info);
        ImageView imageView = (ImageView) dialog.findViewById(R.id.imageView);
        TextView tv_bankcart_info = (TextView) dialog.findViewById(R.id.tv_bankcart_info);
        tv_bankcart_info.setText(info);
        Button bt_queding = (Button) dialog.findViewById(R.id.bt_queding);
        if (code == 0) {
            imageView.setImageResource(R.drawable.img_success);
            tv_bankcart_info.setTextColor(0xFFF77F96);
            bt_queding.setText("返回我的P兜");
        } else {
            imageView.setImageResource(R.drawable.img_fail);
            tv_bankcart_info.setTextColor(0xFF999999);
            bt_queding.setText("重试");
        }
        bt_queding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (l != null) {
                    l.onClick(v);
                }
                dialog.dismiss();
            }
        });
        dialog.findViewById(R.id.img_search).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (code == 0) {
                    context.finish();
                }
                dialog.dismiss();
            }
        });
        dialog.show();
        dialog.setCanceledOnTouchOutside(false);
    }

    /**
     *  成功 单按钮，（无取消键的）
     * @param context
     * @param info
     * @param butten
     * @param l
     */
    public static void showSuccessDilog(Context context,int bg, int color, String info, String butten, final OnClickListener l) {
        final Dialog dialog = new Dialog(context, R.style.dialog_tran);
        dialog.setContentView(R.layout.dialog_success_info);
        ImageView imageView = (ImageView) dialog.findViewById(R.id.imageView);
        TextView tv_bankcart_info = (TextView) dialog.findViewById(R.id.tv_bankcart_info);
        if (color==0){
            tv_bankcart_info.setTextColor(0xFFF77F96);
        }else {
            tv_bankcart_info.setTextColor(0xFF999999);
        }
        tv_bankcart_info.setText(info);
        Button bt_queding = (Button) dialog.findViewById(R.id.bt_queding);
        imageView.setImageResource(bg);
        bt_queding.setText(butten);
        bt_queding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (l != null) {
                    l.onClick(v);
                }
                dialog.dismiss();
            }
        });
        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(false);
        dialog.show();
    }

    /**
     * 定期或转让 售罄 的dialog
     *
     * @param context
     * @param info
     * @param butten
     * @param l
     */
    public static void allSoldOutDialog(Context context, String info, String butten, final OnClickListener l) {
        final Dialog dialog = new Dialog(context, R.style.dialog_tran);
        dialog.setContentView(R.layout.dialog_all_soldout);
        TextView tv_info = (TextView) dialog.findViewById(R.id.tv_info);
        tv_info.setText(info);
        Button bt_queding = (Button) dialog.findViewById(R.id.bt_queding);
        bt_queding.setText(butten);
        bt_queding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (l!=null){
                    l.onClick(v);
                }
                dialog.dismiss();
            }
        });
        dialog.show();
        dialog.setCanceledOnTouchOutside(false);
    }
    /**
     * sp或者 dp 装换为 px
     */
    public static int dpToPx(Context context, int dpValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return Math.round(dpValue * scale);
    }

    /**
     * 工具方法
     *
     * @param context
     * @return
     */
    public static int getScreenWidth(Context context) {
        DisplayMetrics dm = new DisplayMetrics();
        ((Activity) context).getWindowManager().getDefaultDisplay()
                .getMetrics(dm);
        return dm.widthPixels;
    }

    // ----------------------------------------以上为服务以上对话框的工具类--------------------

    // ----------------------------------------以上为界面切换的工具类--------------------

    /**
     * 通过startActivityForResult 激活的界面，返回数据
     *
     * @param context
     * @param resultCode
     */
    public static void resultActivity(Context context, int resultCode) {
        resultActivity(context, resultCode, null);
    }

    public static void resultActivity(Context context, int resultCode,
                                      Map<String, Object> extras) {
        Intent i = new Intent();
        putExtras(extras, i);
        ((Activity) context).setResult(resultCode, i);
        ((Activity) context).finish();

    }

    /**
     * startActivityForResult 激活方式
     *
     * @param from
     * @param to
     * @param requestCode
     */
    public static void switcherFor(Activity from, Class<?> to, int requestCode) {
        switcherFor(from, to, requestCode, null);
    }

    public static void switcherFor(Activity from, Class<?> to, int requestCode,
                                   Map<String, Object> extras) {
        Intent i = new Intent(from, to);
        putExtras(extras, i);
        ((Activity) from).startActivityForResult(i, requestCode);
    }

    /**
     * startActivity 激活ui
     *
     * @param from
     * @param to
     */
    public static void switcher(Activity from, Class<?> to) {
        switcher(from, to, null);
    }

    public static void switcher(Context from, Class<?> to,
                                Map<String, Object> extras) {
        Intent i = new Intent(from, to);
        putExtras(extras, i);
        from.startActivity(i);
    }


    /**
     * intent 中 传递d数据
     *
     * @param extras
     * @param i
     */
    private static void putExtras(Map<String, Object> extras, Intent i) {
        if (extras != null) {
            Bundle b = new Bundle();
            for (String name : extras.keySet()) {
                Object obj = extras.get(name);
                if (obj instanceof String) {
                    i.putExtra(name, (String) obj);
                }
                if (obj instanceof Integer) {
                    i.putExtra(name, (Integer) obj);
                }
                if (obj instanceof String[]) {
                    i.putExtra(name, (String[]) obj);
                }
                if (obj instanceof Serializable) {
                    b.putSerializable(name, (Serializable) obj);
                }
            }
            i.putExtras(b);
        }
    }


}
